<script setup>
//import {excelApi} from "../../../api/axios";
//import {MINIO_HOST} from "../../../const";
import {onMounted, ref, shallowReactive, shallowRef} from "vue";
import router from "../../../router";
import {dateFormat, myRemove, myPage, myRemoveBatch} from "../../../util";
import {pageApi, removeApi, removeBatchApi} from "../../../api/room.js";
import MyNav from "../../../components/MyNav.vue";
import MyHead from "../../../components/MyHead.vue";
import MyTable from "../../../components/MyTable.vue";
import MyPager from "../../../components/MyPager.vue";
import MyList from "../../../components/MyList.vue";

/* ==================== 分页查询 ==================== */
// 表格数据 + 分页数据
let records = ref();
let pageInfo = shallowReactive({'pageNum': 1, 'pageSize': 5, 'fn': page});
// 分页查询条件字段：教室名称
let roomName = shallowRef();

// 分页函数
async function page(pageNum = pageInfo['pageNum'], pageSize = pageInfo['pageSize']) {

  // 分页基础配置
  let config = {
    'api': pageApi,
    'params': {pageNum, pageSize},
    'records': records,
    'pageInfo': pageInfo
  }

  // 附加为分页条件
  if (roomName.value) config['params']['name'] = roomName.value;

  // 发送分页请求
  await myPage(config);
}

/* ==================== 查看详情 ==================== */
// 教室详情抽屉 + 教室详情列表数据
let detailDrawer = shallowRef();
let detailItems = ref();

function detail(row) {
  detailItems.value = [
    {'label': '教室名称', 'value': row['name']},
    {'label': '教室别名', 'value': row['alias']},
    {'label': '教室状态', 'value': row['status'], 'type': 'tag', 'format': roomStatusFormat},
    {'label': '创建时间', 'value': row['created'], 'format': dateFormat},
    {'label': '修改时间', 'value': row['updated'], 'format': dateFormat},
    {'label': '教室描述', 'value': row['info'], 'type': 'card'},
  ];
  detailDrawer.value = true;
}

/**
 * 对教室状态字段status进行处理
 *
 * @param status 状态码，支持 number 或 string 类型
 * @return {string} 0可用，1禁用，其他未知
 */
function roomStatusFormat(status) {
  if (status === 0 || status === '0') return '可用';
  if (status === 1 || status === '1') return '禁用';
  return '未知状态';
}

/* ==================== 单条添加 ==================== */
function insert() {
  router.push('/RoomInsert');
}

/* ==================== 单条修改 ==================== */
function update(row) {
  sessionStorage.setItem('room', JSON.stringify(row));
  router.push('/RoomUpdate');
}

/* ==================== 单条删除 ==================== */
function remove(row) {
  myRemove({'id': row['id'], 'api': removeApi, 'fn': page});
}

/* ==================== 批量删除 ==================== */
// 批量删除主键数组
let ids = [];

function selectionChange(rows) {
  ids = rows.map(e => e['id']);
}

function removeBatch() {
  myRemoveBatch({'ids': ids, 'api': removeBatchApi, 'fn': page})
}

/* ==================== 按教室名称模糊查询 ==================== */
function pageLikeName(val) {

  // 仅当输入框有值，或者当前处于按条件分页状态时，发送分页请求
  if (val || roomName.value) {
    roomName.value = val;
    page();
  }
}

/* ==================== 打印报表 ==================== */
function excel() {
  // excelApi('/room/excel', '教室统计表');
}

/* ==================== 加载函数 ==================== */
onMounted(() => page());

</script>

<template>

  <my-nav :items="[
    {'icon': 'Avatar', 'label': '课程管理'},
    {'icon': 'User', 'label': '教室列表'},
  ]"/>

  <my-head :items="[
    {'type': 'ipt', 'span': 5, 'placeholder': '按教室名称模糊搜索', 'fn': pageLikeName},
    {'type': 'insertBtn', 'offset': 16, 'fn': insert},
    {'type': 'deleteBtn', 'fn': removeBatch},
    {'type': 'excelBtn', 'fn': excel},
  ]"/>

  <my-table v-if="records" :records="records" :columns="[
    {'column': ['教室信息', '教室名称'], 'property': 'name', 'width': 120},
    {'column': ['教室信息', '教室别名'], 'property': 'alias', 'width': 120},
    {'column': ['教室信息', '教室状态'], 'property': 'status', 'type': 'tag', 'format': roomStatusFormat},
    {'column': ['教室信息', '教室描述'], 'property': 'info', 'type': 'card', 'tooltip': false},
  ]" :buttons="[
    {'type': 'info', 'fn': detail},
    {'type': 'update', 'fn': update},
    {'type': 'delete', 'fn': remove}
  ]" :selection-change="selectionChange"/>

  <my-pager v-if="records" :page-info="pageInfo"/>

  <el-drawer v-model="detailDrawer" size="30%">
    <my-list title="记录详情" :items="detailItems"/>
  </el-drawer>

</template>

<style scoped lang="scss">

</style>